/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doa;

import java.util.ArrayList;
import java.util.List;
import models.Message;
import models.Twitterer;
import org.hibernate.Query;
import org.hibernate.Transaction;

/**
 *
 * @author Misja
 */
public class MessageDAO {

    public static List<Message> getMessages() {
        List<Message> Messages = new ArrayList<Message>();
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            Messages = HibernateSessionController.Instance().getHibernateSession().createQuery("from Message").list();
            tx.commit();
        } catch (Exception e) {
        }
        return Messages;
    }

    public static List<Message> getMessagesByOwner(Twitterer owner) {
        List<Message> Messages = new ArrayList<Message>();
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            Query query = HibernateSessionController.Instance().getHibernateSession().createQuery("from Message where owner = :owner ");
            query.setParameter("owner", owner);
            Messages = query.list();
            tx.commit();
        } catch (Exception e) {
        }
        return Messages;
    }

    public static boolean saveMessage(Message message) {
        boolean result = false;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            HibernateSessionController.Instance().getHibernateSession().save(message);
            tx.commit();
            result = true;
        } catch (Exception e) {
            System.out.println("exeptie message" + e);
        }
        System.out.println("" + message.getOwner().getUserName());
        return result;
    }

    public static boolean updateMessage(Message message) {
        boolean result = false;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            HibernateSessionController.Instance().getHibernateSession().flush();
            HibernateSessionController.Instance().getHibernateSession().merge(message);
            tx.commit();
            result = true;
        } catch (Exception e) {
        }
        return result;
    }

    public static long countMessageByOwner(Twitterer owner) {
        long count = 0;      
        try {
            
            count = (Long)HibernateSessionController.Instance().getHibernateSession().createQuery("SELECT COUNT(e) FROM Message e where owner = '" + owner.getId() + "'").uniqueResult();
        } catch (Exception e) {
           
        }
        return count;
    }
    
     public static boolean deleteMessage(Message msg) {
        boolean result = false;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            HibernateSessionController.Instance().getHibernateSession().flush();
            HibernateSessionController.Instance().getHibernateSession().delete(msg);
            tx.commit();
            result = true;
        } catch (Exception e) {
        }
        return result;
    }
    
}
